package com.itheima;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author wmh
 * @version 1.0 2022/5/26
 * @Description: 有样式的excel
 * 边框线，合并单元格，行高列宽，对齐方式，字体
 **/
public class POIDemo4 {


    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        /**
         * 大标题行
         */
        Row bigTitleRow = sheet.createRow(0);

        //1.边框线(全边框)，行高(42)，合并单元格(第一行第一个到第五个单元格)，对齐方式(水平垂直居中)，字体：黑体18号
        CellStyle bigTitleRowCellStyle = workbook.createCellStyle();

        //上下左右边框
        bigTitleRowCellStyle.setBorderTop(BorderStyle.THIN);//BorderStyle.THIN细线
        bigTitleRowCellStyle.setBorderBottom(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderLeft(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderRight(BorderStyle.THIN);
        //对齐方式，水平居中对齐，垂直居中对齐
        bigTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER);
        bigTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        //设置行高
        bigTitleRow.setHeightInPoints(42);

        //设置列宽
        sheet.setColumnWidth(0,5*256);
        sheet.setColumnWidth(1,10*256);
        sheet.setColumnWidth(2,10*256);
        sheet.setColumnWidth(3,10*256);
        sheet.setColumnWidth(4,30*256);

        //字体
        //1.创建字体
        Font font = workbook.createFont();
        //2.设置字体样式
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 18);
        //3.字体放入样式中
        bigTitleRowCellStyle.setFont(font);

        //--5个单元格合并
        for (int i = 0; i < 5; i++) {
            Cell cell = bigTitleRow.createCell(i);
            cell.setCellStyle(bigTitleRowCellStyle);
        }
        //起始行，结束行，开始列，结束列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));

        //向单元格中添加句子
        bigTitleRow.getCell(0).setCellValue("用户信息数据");



        /**
         * 小标题行
         */
        Row littleTitleRow = sheet.createRow(1);

        //1.边框线(全边框)，行高(42)，合并单元格(第一行第一个到第五个单元格)，对齐方式(水平垂直居中)，字体：黑体18号
        CellStyle littleTitleRowCellStyle = workbook.createCellStyle();

        //克隆大标题的样式
        //上下左右边框
        //对齐方式，水平居中对齐，垂直居中对齐
        littleTitleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);

        //设置列宽
        littleTitleRow.setHeightInPoints(32);
        //字体
        //1.创建字体
        Font littleFont = workbook.createFont();
        //2.设置字体样式
        littleFont.setFontName("宋体");
        littleFont.setFontHeightInPoints((short) 12);
        littleFont.setBold(true);
        //3.字体放入样式中
        littleTitleRowCellStyle.setFont(littleFont);


        //添加小标题内容
        String[] titles = new String[]{"编号","姓名","手机号","入职日期","现住址"};
        for (int i = 0; i < 5; i++) {
            Cell cell = littleTitleRow.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(littleTitleRowCellStyle);
        }

        /**
         * 内容样式
         */
        CellStyle contentRowCellStyle = workbook.createCellStyle();
        contentRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        contentRowCellStyle.setAlignment(HorizontalAlignment.LEFT);
        Font contentFont = workbook.createFont();
        contentFont.setFontName("宋体");
        contentFont.setFontHeightInPoints((short) 11);
        contentFont.setBold(false);
        contentRowCellStyle.setFont(contentFont);

        Row contentRow = sheet.createRow(2);
        String[] content = new String[]{"1","姓名1","123456789458","2021-12-05","现住址￥￥￥"};
        for (int i = 0; i < 5; i++) {
            Cell cell = contentRow.createCell(i);
            cell.setCellValue(content[i]);
            cell.setCellStyle(contentRowCellStyle);
        }

        workbook.write(new FileOutputStream("D:/样式表格.xlsx"));
    }

}